﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
public partial class admin_现金助学_贫困生登记 : BLL.BasePage_Admin
{
    Appeal bll_Appeal = new Appeal();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            UIInit();
        }
    }

    private void UIInit()
    {
        PublicControlBind.Bind_DLL_NationalityList(ddlNationality);
        if (Request.QueryString["id"] != null)
        {
            Bind_Detail();
        }
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (!MyValidate())
        {
            return;
        }
        if (Request.QueryString["id"] == null)
        {
            Model.Appeal m = new Model.Appeal();
            FillModel(m);
            m.InsertVolunteerId = CurrentUser.Id;
            m.InsTime = DateTime.Now;
            m.Id = bll_Appeal.Add(m);
            //添加一个审核任务
            Add_LogCheck(m.Id);
        }
        else
        {
            int id = QueryString_Int("id");
            Model.Appeal m = bll_Appeal.GetModel(id);
            FillModel(m);
            bll_Appeal.Update(m);
        }
        Alert_Redirect("保存成功！", "贫困生审核并安排走访.aspx");
    }

    private void Add_LogCheck(int id)
    {
        Model.Log_Check m = new Model.Log_Check();
        m.ObjectId = id;
        m.ObjectType = Convert.ToInt32(PublicEnum.ObjectType_Check.现金助学求助信息录入后的审核);
        Model.DefaultDriver dd = new BLL.DefaultDriver().GetModel(Convert.ToInt32(PublicEnum.DefaultDriver_ID.现金助学_登记后的默认审核人));
        m.User_CurrId = dd.UserId;
        m.User_CurrName = dd.NickName;
        m.User_FromId = 0;
        m.User_FromName = "系统";
        new BLL.Log_Check().Add(m);
    }

    private void FillModel(Model.Appeal m)
    {
        m.Address = txtAddress.Text.Trim();
        if (!string.IsNullOrEmpty(txtBirthday.Text.Trim()))
        {
            m.Birthday = Convert.ToDateTime(txtBirthday.Text.Trim());
            m.Age = Common.Helper_DateTime.Get_Age(m.Birthday);
        }
        m.Demand = txtDemand.Text.Trim();
        m.DistrictId = Convert.ToInt32(Prov_City_Dist1.DistrictId);
        m.FamilyIncome = txtFamilyIncome.Text.Trim();
        m.FamilyIncomeFrom = txtFamilyIncomeFrom.Text.Trim();
        m.FamilyInfo = txtFamilyInfo.Text.Trim();
        m.Grade_Class = txtGrade_Class.Text.Trim();
        m.Guardian1 = txtGuardian1_Name.Text.Trim() + "|" + txtGuardian1_Relation.Text.Trim() + "|" + txtGuardian1_Link.Text.Trim();
        m.Guardian2 = txtGuardian2_Name.Text.Trim() + "|" + txtGuardian2_Relation.Text.Trim() + "|" + txtGuardian2_Link.Text.Trim();
        m.Name = txtName.Text.Trim();
        m.Nationality = ddlNationality.SelectedValue;
        m.PostalAddress = txtPostalAddress.Text.Trim();
        m.School = txtSchool.Text.Trim();
        m.Sex = ddlSex.Text;
        m.Teacher = txtTeacher_Name.Text.Trim() + "|" + txtTeacher_Link.Text.Trim();
        m.From = txtFrom.Text.Trim();
    }

    private bool MyValidate()
    {
        try
        {
            Check_NotEmpty(txtName, "请输入姓名！");
            Check_NotEmpty(ddlSex, "请选择性别！");
            Check_NotEmpty(ddlNationality, "请选择民族！");
            Check_NotEmpty(Prov_City_Dist1.DDL_District, "请选择地区！");
            Check_NotEmpty(txtPostalAddress, "请填写通讯地址！");
            Check_NotEmpty(txtFamilyInfo, "请填写家庭状况描述！");
            Check_NotEmpty(txtDemand, "请填写需求！");
            Check_NotEmpty(txtSchool, "请填写学校！");
            Check_NotEmpty(txtGrade_Class, "请填写年级班级！");
        }
        catch (Exception ex)
        {
            Alert(ex.Message);
            return false;
        }
        return true;
    }
    private void Bind_Detail()
    {
        int id = QueryString_Int("id");
        Model.Appeal m = bll_Appeal.GetModel(id);
        txtAddress.Text = m.Address;
        txtBirthday.Text = string.IsNullOrEmpty(m.Birthday.ToString()) ? "" : Convert.ToDateTime(m.Birthday).ToString("yyyy-MM-dd");
        txtDemand.Text = m.Demand;
        txtFamilyIncome.Text = m.FamilyIncome;
        txtFamilyIncomeFrom.Text = m.FamilyIncomeFrom;
        txtFamilyInfo.Text = m.FamilyInfo;
        txtGrade_Class.Text = m.Grade_Class;
        
        txtGuardian1_Link.Text = m.Guardian1_Link;
        txtGuardian1_Name.Text = m.Guardian1_Name;
        txtGuardian1_Relation.Text = m.Guardian1_Relation;
        txtGuardian2_Link.Text = m.Guardian2_Link;
        txtGuardian2_Name.Text = m.Guardian2_Name;
        txtGuardian2_Relation.Text = m.Guardian2_Relation;
        txtName.Text = m.Name;
        txtPostalAddress.Text = m.PostalAddress;
        txtSchool.Text = m.School;
        txtTeacher_Link.Text = m.Teacher_Link;
        txtTeacher_Name.Text = m.Teacher_Name;
        ddlNationality.SelectedValue = m.Nationality;
        ddlSex.Text = m.Sex;
        Prov_City_Dist1.DistrictId = m.DistrictId.ToString();
    }
}